package org.jadestudio.home.action;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.jadestudio.home.dao.CommonDao;
import org.jadestudio.home.service.ArticleService;
import org.jadestudio.home.service.LeaderboardService;

public class ArticleDetailAction extends Action {
	private ArticleService articleService;
	private LeaderboardService leaderboardService;
	
	public ArticleService getArticleService() {
		return articleService;
	}
	public void setArticleService(ArticleService articleService) {
		this.articleService = articleService;
	}
	
	public LeaderboardService getLeaderboardService() {
		return leaderboardService;
	}
	public void setLeaderboardService(LeaderboardService leaderboardService) {
		this.leaderboardService = leaderboardService;
	}
	
	/**
	 * 加载数据
	 */
	@SuppressWarnings("unchecked")
	public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception{
		
		String id = request.getParameter("id");
		int page = request.getParameter("page")==null?1:Integer.parseInt(request.getParameter("page"));
		
		String ip = CommonDao.getIP(request);
		
		// 加载文章详细信息
		java.util.Map articleDetail = articleService.getArticleDetail(id);
		if (articleDetail!=null){
			String article_content = articleDetail.get("article_content").toString();

			// 解析并切割文章内容
			Pattern pattern = Pattern.compile("\\[page=[^\\]]*\\]");
			Matcher matcher = pattern.matcher(article_content);
			int pagecount = matcher.groupCount();
			
			int position = 0;
			String page_content = "";
			List<String> titles = new ArrayList<String>();
			while(matcher.find(position)){
				titles.add(matcher.group().substring(6, matcher.group().length()-1));
				if (page==titles.size()-1){
					page_content = article_content.substring(position,matcher.start());
				}
				position=matcher.end();
			}
			if (page>=titles.size()-1)
				page_content=article_content.substring(position,article_content.length());
			
			// 相关游戏
			List<java.util.Map> articleGameList = articleService.getArticleGameList(id);
			
			// 投票情况
			java.util.Map articleVoteList = articleService.getArticleVotes(id,ip);
			
			// 排行榜
			List<java.util.Map> articleTop10 = leaderboardService.getArticleTop10();
			
			// 相关文章
			List<java.util.Map> articleSimilar = articleService.getSimilarArticleList(id);

			request.setAttribute("titles", titles);
			articleDetail.put("article_content", page_content);
			request.setAttribute("articleDetail", articleDetail);
			request.setAttribute("articleTop10", articleTop10);
			request.setAttribute("articleGameList", articleGameList);
			request.setAttribute("articleVoteList", articleVoteList);
			request.setAttribute("articleSimilar", articleSimilar);

			return mapping.findForward("success");
		}else{
			return mapping.findForward("nosucharticle");
		}
	}
}
